from scipy.signal import butter,bessel,filtfilt

def high_pass_filter(numlist, cutoff, sample_rate, filter_order=5, filter_type='butter'):
    """
    高通滤波器
    numlist:      data need to be filter
    cutoff:       cutoff rate(Hz)
    sample_rate:  sample rate of data(Hz)
    filter_order: order of filter you choose
    filter_type:  'butter' or 'bessel'
    """
    fs = sample_rate
    fc = cutoff  # Cut-off frequency of the filter
    w = fc / (fs / 2)  # Normalize the frequency
    if filter_type == 'butter':
        b, a = butter(filter_order, w, 'high')
    elif filter_type == 'bessel':
        b, a = bessel(filter_order, w, 'high')
    else:
        raise TypeError("Filter type is not supported!")
    output = filtfilt(b, a, numlist)
    return output


def low_pass_filter(numlist, cutoff, sample_rate, filter_order=5, filter_type='butter'):
    """
    低通滤波器
    numlist:      data need to be filter
    cutoff:       cutoff rate(Hz)
    sample_rate:  sample rate of data(Hz)
    filter_order: order of filter you choose
    filter_type:  'butter' or 'bessel'
    """
    fs = sample_rate
    fc = cutoff  # Cut-off frequency of the filter
    w = fc / (fs / 2)  # Normalize the frequency
    if filter_type == 'butter':
        b, a = butter(filter_order, w, 'low')
    elif filter_type == 'bessel':
        b, a = bessel(filter_order, w, 'low')
    else:
        raise TypeError("Filter type is not supported!")
    output = filtfilt(b, a, numlist)

    return output


#对原始信号进行滤波留下心率的波形
def heart_filter(numlist,high_cutoff = 1.0,low_cutoff = 10.0,sample_rate=50):
    beating = numlist
    beating = high_pass_filter(beating, cutoff=high_cutoff, filter_order=2, sample_rate=sample_rate)
    beating = low_pass_filter(beating, cutoff=low_cutoff, filter_order=2, sample_rate=sample_rate)

    return beating


#对原始信号进行滤波留下呼吸的波形
def breathe_filter(numlist,high_cutoff = 0.1,low_cutoff = 1.0,sample_rate=50):
    breathing = numlist
    #breathing = high_pass_filter(breathing, cutoff=high_cutoff,filter_order=8,sample_rate=sample_rate)
    breathing =low_pass_filter(breathing,cutoff = low_cutoff,filter_order=8,sample_rate=sample_rate)

    return breathing